#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <time.h>

typedef struct userdata
{
	int code;
	char username[32];
	char password[32];
	char name[32];
	char time[128];
	char text[256];
}tcpsock;
typedef struct usermes
{
	int code;
	char name[32];
	char time[128];
	char text[256];
}user_mes;

char loginsuccname[32];
user_mes sendusermes;
user_mes recvusermes;
int main(int argc, const char *argv[])
{
	int sockfd=socket(AF_INET,SOCK_STREAM,0);
	perror("socket");
	struct sockaddr_in serveraddr;
	serveraddr.sin_addr.s_addr=inet_addr(argv[1]);
	serveraddr.sin_port=htons(atoi(argv[2]));
	serveraddr.sin_family=AF_INET;

	socklen_t len =sizeof(serveraddr);
	connect(sockfd,(struct sockaddr *)&serveraddr,len);
	perror("connect");
	char sendbuf[256]={0};
	int loginchoosenum=0;
	int userchoosenum=0;
	char usertime[128];
	char recvusername[34];
	char sendusername[32];
	tcpsock sendloginbuf;
	tcpsock recvloginornot;
	time_t getsec;
	int chooseviewmes;
	int i=0;
	int viewallmeskey=0;
	while(1)
	{
		viewallmeskey=0;
		printf("*******************************\n");
		printf("***********1.regst***********\n");
		printf("***********2.login***********\n");
		printf("*******************************\n");
		scanf("%d",&loginchoosenum);
		getchar();
		switch(loginchoosenum)
		{
			case 1:
				sendloginbuf.code=1;
				printf("plz input your name\n");
				fgets(sendloginbuf.username,32,stdin);
				printf("plz input your password\n");
				fgets(sendloginbuf.password,32,stdin);
				send(sockfd,&sendloginbuf,sizeof(sendloginbuf),0);
				memset(sendloginbuf.name,0,32);
				memset(sendloginbuf.password,0,32);
				memset(sendloginbuf.text,0,256);
				memset(sendloginbuf.time,0,128);
				memset(sendloginbuf.username,0,128);
				break;
			case 2:
				sendloginbuf.code=2;
				printf("plz input your name\n");
				fgets(sendloginbuf.username,32,stdin);
				printf("plz input your password\n");
				fgets(sendloginbuf.password,32,stdin);
				send(sockfd,&sendloginbuf,sizeof(sendloginbuf),0);
				memset(sendloginbuf.name,0,32);
				memset(sendloginbuf.password,0,32);
				memset(sendloginbuf.text,0,256);
				memset(sendloginbuf.time,0,128);
				memset(sendloginbuf.username,0,128);

				printf("wairecv loginornot\n");
				recv(sockfd,&recvloginornot,sizeof(recvloginornot),0);
				printf("recv loginornot\n");
				if(recvloginornot.code==0)
				{
					printf("welcome %s \n",recvloginornot.username);
					strcpy(loginsuccname,recvloginornot.username);
					goto loginsucc;
				}
				else if(recvloginornot.code==1)
				{
					printf("login error\n");
				}
				break;
			default:
				break;
		}

	}

loginsucc:
	while(1)
	{
		viewallmeskey=0;
		printf("************************************************\n");
		printf("*********1.write**2.viewusermes**3.allmes**********\n");
		printf("************************************************\n");
		scanf("%d",&userchoosenum);
		getchar();
		switch(userchoosenum)
		{
			case 1:
				sendloginbuf.code=3;
				strcpy(sendloginbuf.name,loginsuccname);
				printf("plz input text \n");
				scanf("%s",sendloginbuf.text);
				getsec=time(NULL);
				strcpy(usertime,ctime(&getsec));
				strcpy(sendloginbuf.time,usertime);

				send(sockfd,&sendloginbuf,sizeof(sendloginbuf),0);
				memset(sendloginbuf.name,0,32);
				memset(sendloginbuf.password,0,32);
				memset(sendloginbuf.text,0,256);
				memset(sendloginbuf.time,0,128);
				memset(sendloginbuf.username,0,128);
				memset(usertime,0,128);
				break;
			case 2:
				sendloginbuf.code=4;
				send(sockfd,&sendloginbuf,sizeof(sendloginbuf),0);
				while(0!=recv(sockfd,&recvusername,sizeof(recvusername),0))
				{
					if(recvusername[0]=='1')
					{
						printf("****  end  ****\n");
						memset(recvusername,0,34);
						break;
					}
					else
					{
						printf("++++ %s ++++\n",recvusername+2);
						memset(recvusername,0,34);
					}
				}
				printf("plzinput name\n");
				scanf("%s",sendusername);
				send(sockfd,&sendusername,sizeof(sendusername),0);
				memset(sendusername,0,32);
				
				break;
			case 3:
				sendloginbuf.code=4;
				send(sockfd,&sendloginbuf,sizeof(sendloginbuf),0);
				recv(sockfd,&recvusermes,sizeof(recvusermes),0);
				if(recvusermes.code==-2)
				{
					printf("*******no message!!\n********");
				}
				while(viewallmeskey==0)
				{
					printf("++++ 1.view 2.cancel ++++\n");
					scanf("%d",&chooseviewmes);
					switch(chooseviewmes)
					{
						case 1:
							sendusermes.code=1;
							send(sockfd,&sendusermes,sizeof(sendusermes),0);
							for(i=0;i<10;i++)
							{
								recv(sockfd,&recvusermes,sizeof(recvusermes),0);
								printf("----\n");
								printf("****%s--%s\n",recvusermes.name,recvusermes.time);
								printf("****%s\n",recvusermes.text);
								printf("----\n");
								if(recvusermes.code==2)
								{
									printf("*******no message!!\n********");
									break;
								}
							}
							break;
						case 2:
							sendusermes.code=0;
							send(sockfd,&sendusermes,sizeof(sendusermes),0);
							viewallmeskey=1;
							break;
						default:
							break;
					}
				}
				break;
		}

	}

	return 0;
}
